const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property
    baseUrl = "https://dqdz.chaotianluo.com/";
    wx = window["wx"];
    //onLoad () {}

    //start () {}

    // update (dt) {}

    
    //显示加载，转圈
    wx_loading(title)
    {        
        this.wx.showLoading({
            title: title,
            mask : true
        });        
    }

    //关闭加载
    wx_close_loading()
    {        
        this.wx.hideLoading();  
    }

    //显示提示信息
    wx_tips(context,flag)
    {        
        var icon = null;
        if(flag)
        {
            icon =  'success';
        }
        else
        {
            icon =  'none';
        }
        this.wx.showToast({
            title: context,
            icon: icon,
            mask: true,
            duration: 2000
        })
    }

    //分享出去
    btn_share(title,image_url,query)
    {          
        return new Promise((resolve, reject) => {
            var time_out = setTimeout(() => {
                resolve('out');
               }, 3000);

            this.wx.shareAppMessage({
                title: title,
                imageUrl: image_url,
                query: query,
                success: function (res) {
                    clearTimeout(time_out)
                    resolve(res);
                },
                fail: function (res) {
                    clearTimeout(time_out)
                    resolve(res);                    
                }                
            })               
        })
    }

    //头部按键分享
    meun_share(title,image_url,query)
    {
        this.wx.onShareAppMessage(function(){
            return{
                title: title,
                imageUrl: image_url,
                query: query,
                success: function (res) {
                    console.log(res);
                },
                fail: function (res) {
                    console.log(res);
                }  
            }  
        })
    }

    //群分享成功,获取转发详细信息，以便得到OPENGID
    wx_share_info(res)
    {        
        return new Promise((resolve, reject) => {
            var time_out = setTimeout(() => {
                resolve('out');
               }, 3000);

            this.wx.getShareInfo({
                shareTicket: res.shareTickets[0],
                success: function(res) {
                    clearTimeout(time_out)
                    resolve(res)
                }                
            })
        })
    }

    //调用该函数，可以分享群回调
    sharegroup()
    {        
        this.wx.showShareMenu({
            withShareTicket: true
        });        
    }

    

    http(url,method,body){
        return new Promise((resolve, reject) => {
            var time_out = setTimeout(() => {
                resolve('out');
               }, 3000);

            this.wx.request({
                url : this.baseUrl + url,
                data:body,
                method: method,
                header: { 
                    "Content-Type":"applciation/json"
                },
                success: res => {
                    clearTimeout(time_out)
                    resolve(res);
                }
            })  
        })  
    }

    //获取微信的code
    get_code(){
        return new Promise((resolve, reject) => {
            this.wx.login({
                success:res =>{  
                    resolve(res);
                }
            }) 
        })
    }

}